<div class="page-header">
    <h1>
        Profile
        <small>
            <i class="icon-double-angle-right"></i>
            Upgrade Member Status
        </small>
    </h1>
</div>

<?php
if ($this->errmsg) {
    ?>
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-remove"></i>
            Error <br>
        </strong>

        <?php echo $this->errmsg; ?>
        <br />
    </div>
    <?php
}

if ($this->msg) {
    ?>
    <div class="alert alert-info">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-ok"></i>
        </strong>

        <?php echo $this->msg; ?>
        <br />
    </div>
    <?php
}
    ?>

    <div class="row">
        <div class="col-xs-11">
            <form id="formAdd" class="form-horizontal" role="form" action="" method="post" onsubmit="return validate()">

                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="package"> Package </label>

                    <div class="col-sm-9">
                            <select class="width-40 chosen-select" id="package" name="package" required>
                                <option value=""></option>
                                <?php
                                while ($this->packages->next()) {
                                    $package = $this->packages->current();
                                    echo '<option value="' . $package['member_type_id'] .'">' . $package['member_type_name'] . '</option>';
                                }
                                ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="plan"> Plan </label>

                    <div class="col-sm-9">
                        <select class="col-sm-5" id="plan" name="plan" placeholder="Select an Option" required>
                            <option value="">Select an Option</option>
                        </select>
                        <label id="spDetailPlan" class="help-inline middle"></label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="currency"> Currency </label>

                    <div class="col-sm-9">
                            <select class="width-40 chosen-select" id="currency" name="currency" required>
                                <!--option value=""></option-->
                                <?php
                                for ($x=0; $x<count($this->currencys); $x++){
                                    echo '<option value="' . $this->currencys[$x]['code'] .'">('. $this->currencys[$x]['code'] .') '. $this->currencys[$x]['name'] . '</option>';
                                }
                                ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="amount">Amount</label>
                    <div class="col-sm-9">
                        <input type="text" id="amount" name="amount" placeholder="Number only" class="col-xs-10 col-sm-5" value="<?php if (isset( $this->data['amount'] )) { echo $this->data['amount'];} ?>" required onkeypress='return validateNumber(event)' />
                    </div>

                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="plan"> Payment </label>

                    <div class="col-sm-9">
                        <select class="width-40 chosen-select" id="payment" name="payment" placeholder="Select an Option" required>
                            <option value=""></option>
                                <?php
                                for ($x=0; $x<count($this->payments); $x++){
                                    echo '<option value="' . $this->payments[$x]['code'] .'">'. $this->payments[$x]['name'] . '</option>';
                                }
                                ?>

                        </select>                        
                    </div>
                </div>
            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-right" for="bank_acc_name">Password </label>
                <div class="col-sm-9">
                    <input type="password" id="passtrans" name="passtrans" placeholder="Password" class="col-xs-10 col-sm-5" required />
                </div>

            </div>
            
            
            <div class="clearfix form-actions">
                <div class="col-md-offset-3 col-md-9">
                    <input type="hidden" id="balance" name="balance" value="<?php echo $this->EwalletBalance;?>" >
                    <button class="btn btn-success" type="submit" id="btnSubmit">
                        <i class="icon-ok bigger-110"></i>
                        Submit
                    </button>

                    &nbsp; &nbsp; &nbsp;
                    <button class="btn" type="reset">
                        <i class="icon-undo bigger-110"></i>
                        Reset
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>

    $("select#package").change(function(){

         var pkg = $("#package").val();
         var options = '';
         var detailPlan = "";

         if(pkg != ""){

             options += '<option value="">== Loading Plan ==</option>';
             $("select#plan").html(options);
             $("select#plan").attr('disabled','disabled');
             $("#btnSubmit").attr('disabled','disabled');

             $.getJSON("<?php echo $this->url() ?>/../../jsondata/getpackagechild",{parent: pkg, ajax: 'true'}, function(j){
                  options = '';
                  options += '<option value="">Select an Option</option>';
                  for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].member_type_id + '">' + j[i].member_type_name + '</option>';
                        detailPlan += '<input type="hidden" id="min'+j[i].member_type_id+'" name="min'+j[i].member_type_id+'" value="'+j[i].min_deposit+'"><input type="hidden" id="max'+j[i].member_type_id+'" name="max'+j[i].member_type_id+'" value="'+j[i].max_deposit+'"><span id="spContentDetail'+j[i].member_type_id+'" class="tooltip-info" data-rel="popover" data-placement="right" title="<i class=\'icon-info-sign blue\'></i>&nbsp;&nbsp;' + j[i].member_type_name + '" data-content="'+ j[i].member_type_desc +'"></span>';

                  }
                  //alert(detailPlan);
                  //$("spDetailPlan").html(detailPlan);
                  document.getElementById("spDetailPlan").innerHTML = detailPlan;
                  $("select#plan").removeAttr('disabled');
                  $("select#plan").html(options);
                  $("#btnSubmit").removeAttr('disabled');
                  $('[data-rel=popover]').popover({html:true});                  
            });
        }
      });


    var lastSelectPlan = "";

    $("select#plan").change(function(){
         var plan = $("#plan").val();
         
         if (lastSelectPlan != "") $("#spContentDetail"+lastSelectPlan).trigger("click");

         $("#spContentDetail"+plan).trigger("click");
         lastSelectPlan = plan;
    });

    function validateNumber(evt) {

        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }

    function validate() {

        var plan = $('#plan').val();
        var amt = $('#amount').val();
        var balance = $('#balance').val();
        var payment = $('#payment').val()

        var msg = "";

        
        if (eval(amt) < eval($('#min'+plan).val()))
            msg = msg + "* Minimum Deposit is $"+$('#min'+plan).val()+"<br>";

        if (eval(amt) > eval($('#max'+plan).val()))
            msg = msg + "* Maximum Deposit is $"+$('#max'+plan).val()+"<br>";

        if (payment == 1 && eval(balance) < eval(amt))
            msg = msg + "* Your E-Wallet Balance is "+balance+"<br>";
        
        if (msg == "")
            return true;
        else {
            //alert("Data tidak lengkap:\n\n" + msg);
            //$.messager.alert("Data tidak lengkap:\n\n" + msg);
            bootbox.dialog({
                    message: "<span class='bigger-110'>"+msg+"</span>",
                    buttons:
                    {
                            "button" :
                             {
                                    "label" : "<i class='icon-ok'></i> Ok",
                                    "className" : "btn-sm btn-primary",
                                    //"callback": function() {
                                            //Example.show("great success");
                                    //}
                            }
                    }
            });
            return false;
        }
    }
</script>
